﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace StudentsInBInaryFiles
{
    class StudentsInBinaryFilesTest
    {
        const string WORKING_FILE = "file.bin";

        static void AddStudentAtTheEndOfTheFile(Student student)
        {
            BinaryWriter outputFile = new BinaryWriter(File.Open(WORKING_FILE, FileMode.Append));
            using (outputFile)
            {
                outputFile.Write(student.FirstName);
                outputFile.Write(student.LastName);
                outputFile.Write(student.FacultyNumber);
            }
        }

        static void PrintAllStudent()
        {
            BinaryReader inputFile = new BinaryReader(File.Open(WORKING_FILE, FileMode.Open));

            using (inputFile)
            {
                
                while (inputFile.PeekChar()!=-1)
                {
                    string firstName = inputFile.ReadString();
                    string lastName = inputFile.ReadString();
                    string facultyName = inputFile.ReadString();
                    Console.WriteLine(firstName);
                    Console.WriteLine(lastName);
                    Console.WriteLine(facultyName);
                    Console.WriteLine();

                }
                
            }

        }

        static void DeleteStudent(Student student)
        {
            BinaryReader inputFile = new BinaryReader(File.Open(WORKING_FILE, FileMode.Open));

            using (inputFile)
            {
                bool isFound = false;
                while(isFound==false && inputFile.PeekChar()!=-1)
                {
                long currentPosition = inputFile.BaseStream.Position;
                string firstName = inputFile.ReadString();
                string lastName = inputFile.ReadString();
                string facultyNumber = inputFile.ReadString();

                if (firstName == student.FirstName && lastName == student.LastName && facultyNumber == student.FacultyNumber)
                {
                    long foundPosition = inputFile.BaseStream.Position;
                    StringBuilder sb = new StringBuilder();
                    while (inputFile.PeekChar() != -1)
                    {
                        sb.Append(inputFile.ReadString());
                    }
                    inputFile.Close();
                    BinaryWriter deletionFile = new BinaryWriter(File.Open(WORKING_FILE, FileMode.Open));
                    using (deletionFile)
                    {
                        deletionFile.Seek((int)currentPosition, SeekOrigin.Begin);

                        for (int i = 0; i < sb.Length; i++)
                        {
                            deletionFile.Write(sb[i]);
                            
                        }
                        isFound = true;
                    }


                }
                    
                }
            }
        }

        static void Main(string[] args)
        {
            Student gosho = new Student("georgi", "petrov", "92311212");
            AddStudentAtTheEndOfTheFile(gosho);
            Student pesho = new Student("petar", "petrov", "92301230");
            AddStudentAtTheEndOfTheFile(pesho);

            PrintAllStudent();
            DeleteStudent(pesho);
            PrintAllStudent();
        }
    }
}
